<!--
 * @Description: 店铺选择列表
 * @Author: liyujie
 * @Date: 2021/08/10
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/08/10
-->
<template>
    <section class="wrapper">
        <el-dialog
            title="选择商家"
            :visible.sync="dialogVisible"
            width="680px"
            :close-on-click-modal="false"
            @close="$emit('close')"
        >
            <div class="dialog-content">
                <div class="filter-view">
                    <div class="header-new-box">
                        <p class="header-new-box-title">
                            客户类型
                        </p>
                        <el-select
                            v-model="filterData.shop_group"
                            placeholder="全部"
                            size="medium"
                            clearable
                            style="width: 136px"
                            @change="getTableData(1)"
                        >
                            <el-option
                                label="全部"
                                :value="0"
                            />
                            <el-option
                                label="未设置"
                                :value="-1"
                            />
                            <el-option
                                v-for="item in shop_group_list"
                                :key="item.shop_group"
                                :label="item.title"
                                :value="item.shop_group"
                            />
                        </el-select>
                    </div>
                    <div class="header-new-box">
                        <p class="header-new-box-title">
                            商家名称
                        </p>
                        <el-input
                            placeholder="请输入商家名称"
                            v-model="filterData.keyword"
                            suffix-icon="el-icon-search"
                            clearable
                            size="medium"
                            style="width: 225px"
                            @change="getTableData(1)"
                            @keydown.enter.native="getTableData(1)"
                        />
                    </div>
                </div>
                <div class="table-wrapper">
                    <el-table
                        :data="tableData"
                        height="100%"
                        style="width: 100%;"
                        ref="table"
                        class="data-table"
                        :cell-style="{height: '40px'}"
                        v-loading="tableLoading"
                        row-key="shop_no"
                        @selection-change="handleSelectionChange"
                    >
                        <el-table-column
                            align="center"
                            width="80"
                            :reserve-selection="true"
                            type="selection"
                        />
                        <el-table-column
                            label="序号"
                            align="center"
                            width="80"
                            type="index"
                        />
                        <el-table-column
                            label="客户类型"
                            prop="shop_group_title"
                            align="center"
                            show-overflow-tooltip
                            :formatter="emptyFormatter"
                        />
                        <el-table-column
                            prop="company_name"
                            label="公司名称"
                            show-overflow-tooltip
                            :formatter="emptyFormatter"
                        />
                        <el-table-column
                            prop="shop_title"
                            label="商家名称"
                            show-overflow-tooltip
                            :formatter="emptyFormatter"
                        />
                    </el-table>
                </div>
                <div class="pagination-wrapper clearfix">
                    <el-pagination
                        layout="total, prev, pager, next, jumper"
                        :current-page.sync="page.now_page"
                        :total="page.total_count"
                        :page-size="page.page_size"
                        @current-change="getTableData"
                    />
                </div>
            </div>
            <div class="dialog-footer">
                <el-button
                    size="medium"
                    style="width: 120px; margin-right: 24px"
                    @click="dialogVisible = false"
                >
                    取消
                </el-button>
                <el-button
                    type="primary"
                    size="medium"
                    style="width: 120px"
                    @click="handleConfirm"
                >
                    确定
                </el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
import Vue from "vue";

const Dialog = {
    name: "ShopSelect",
    props: {
        selectedData: {
            type: Array,
            default: () => {
                return [];
            }
        },
        shopType: {
            type: Number,
            default: 0
        }
    },
    data() {
        return {
            dialogVisible: false,
            filterData: {
                keyword: '',
                shop_group: 0
            },
            tableData: [],
            tableLoading: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            shop_group_list: [],
            multipleSelection: []
        };
    },
    computed: {},
    filters: {},
    mounted() {
        this.getTableData();
        this.getShopGroupList();
        this.multipleSelection = this.selectedData;
        this.$nextTick(() => {
            this.multipleSelection.forEach(row => {
                this.$refs.table.toggleRowSelection(row, true);
            });
        });

    },
    methods: {

        /**
         * 获取客户类型列表
         */
        getShopGroupList() {
            this.$post('/student/shop_group%5Cget_list_for_select', {}, resp => {
                if (resp.code === 1) {
                    this.shop_group_list = resp.data.list;
                }
            });
        },

        /**
         * 显示弹窗
         */
        show() {
            this.dialogVisible = true;
        },

        /**
         * 显示弹窗
         */
        close() {
            this.dialogVisible = false;
        },

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {
            this.tableLoading = true;
            let params = { ...this.filterData };

            params.page_id = page || this.page.now_page;
            params.shop_type = this.shopType;
            this.$post('/student/shop%5Cget_shop_list_for_select', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 重置筛选条件
         */
        handleReset() {
            this.filterData = {
                keyword: '',
                shop_group: 0,
            };
            this.getTableData(1);
        },

        /**
         * 表格多选事件
         * @param val
         */
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },

        /**
         * 确定选择
         */
        handleConfirm() {
            if(!this.multipleSelection.length) {
                this.$message.warning('未选择任何商家');
                return;
            }
            this.$emit('select', this.multipleSelection);
        }
    }
};

export default Dialog;

const shopSelectDialog = function (config) {
    return new Promise((resolve) => {
        const DialogConstructor = Vue.extend(Dialog);
        const instance = new DialogConstructor({
            propsData: config,
        }).$mount();

        instance.$on("close", function () {
            setTimeout(() => {
                document.body.removeChild(instance.$el);
                instance.$destroy();
            }, 300);
            resolve(false);
        });
        instance.$on("select", function (data) {
            instance.close();
            resolve(data);
        });
        document.body.appendChild(instance.$el);
        instance.dialogVisible = true;
    });
};

export { shopSelectDialog };

</script>

<style scoped lang="less">
.wrapper {
    .dialog-content {
        display: flex;
        flex-direction: column;
        .filter-view {
            box-sizing: border-box;
            display: flex;
            align-items: center;
            flex-wrap: wrap;

            .header-new-box {
                display: flex;
                align-items: center;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #333333;
                white-space: nowrap;
                margin-right: 24px;
                margin-bottom: 12px;

                &-title {
                    margin-right: 16px;
                    white-space: nowrap;
                }
                .query-button  {
                    width: 84px;
                    height: 36px;
                    background: #FFFFFF;
                    border-radius: 4px;
                    border: 1px solid #1890FF;
                    font-size: 14px;
                    font-family: PingFangSC-Medium, PingFang SC;
                    font-weight: 500;
                    color: #1890FF;
                    margin-right: 12px;
                }
            }
        }

        // 表格
        .table-wrapper {
            height: 400px;
            background: #FBFBFB;
            border-radius: 4px;
            border: 1px solid #EEEEEE;
            border-bottom: 0;
            .cell-title {
                font-family: PingFangSC-Medium, PingFang SC;
                font-weight: 500;
                color: #333333;
                margin-bottom: 12px;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                overflow: hidden;
                -webkit-line-clamp: 2;
            }
            .cell-content {
                display: -webkit-box;
                -webkit-box-orient: vertical;
                overflow: hidden;
                -webkit-line-clamp: 3;
            }
            .cell-tips {
                color: #999;
            }
        }
        .data-table {
            ::-webkit-scrollbar {
                width: 6px;
                height: 6px;
            }
            // 操作按钮
            .option-btn-group {
                .el-button {
                    margin-left: 0;
                    margin-right: 10px;
                }
            }
        }
        .status-wrapper {
            display: flex;
            align-items: center;
            justify-content: center;

            .status-dot {
                width: 6px;
                height: 6px;
                border-radius: 50%;
                margin-right: 5px;

                &.success {
                    background: #4CD964;
                }

                &.danger {
                    background: #FF3030;
                }

                &.warning {
                    background: #FFA72D;
                }

                &.normal {
                    background: #A6A6A6;
                }
            }
        }
    }
    .dialog-footer {
        display: flex;
        justify-content: center;
        margin-top: 12px;
    }

    /* 分页 */
    .pagination-wrapper {
        padding: 12px;
        background: #FBFBFB;
        border-radius: 0 0 4px 4px;
        border: 1px solid #eee;
        border-top: 0;
        /deep/ .el-pagination {
            float: right;
            padding: 0;
            background-color: #FBFBFB;
            .el-pager {
                .number, .more {
                    background-color: #FBFBFB;
                }
            }
            .btn-next, .btn-prev {
                background-color: #FBFBFB;
            }
        }
    }

    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow:0 1px 0 0 rgba(233,233,233,1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;
        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }
        .el-icon-close:before {
            color: rgb(194,194,194);
            font-size: 17px;
            font-weight: bold;
        }
        .el-dialog__headerbtn {
            top: 14px;
        }
    }
    /deep/ .el-dialog__body {
        padding: 24px 32px;
    }
    /deep/ .el-dialog {
        margin-top: 10vh!important;
    }
    /deep/ .el-table {
        th {
            background: #f2f2f2!important;
            font-size: 14px!important;
            font-family: PingFangSC-Medium, PingFang SC!important;
            font-weight: 500!important;
            color: #333333!important;
            height: 44px!important;
        }
    }
}
</style>
